﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace OTDR_OPT
{
    /// <summary>
    /// CheckPointEdit.xaml 的交互逻辑
    /// </summary>
    public partial class CheckPointEdit : Window
    {
        public CheckPointEdit()
        {
            InitializeComponent();
            InitProductList();
        }


        //读取机型清单，显示在列表中
        private void InitProductList()
        {
            List<string> productList = ProductManagement.GetInstance().GetProductList();
            ProductNameDataGrid.ItemsSource = null;
            ProductNameDataGrid.ItemsSource = productList;
        }


        //保存按钮点击时，保存配置
        private void SaveButton_Click(object sender,RoutedEventArgs args) {
            Dictionary<string, List<CheckPoint>> checkDic = new Dictionary<string, List<CheckPoint>>();
            foreach(TabItem tab in CheckPointTab.Items)
            {
                string wavelength = tab.Header.ToString().Trim(); //获取波长
                Grid grid = (Grid)((ScrollViewer)tab.Content).Content; //获取波长下的容器
                //遍历容器下的所有LinearControl控件，取得复查点信息
                List<CheckPoint> checkList = new List<CheckPoint>();
                foreach(object obj in grid.Children)
                {
                    LinearControl linearControl = obj as LinearControl;
                    CheckPoint check = linearControl.GetChecnPoint();
                    if(check != null)
                    {
                        checkList.Add(check);
                    }
                }
                checkDic.Add(wavelength,checkList);
            }
            string productName = ProductNameBox.Text.Trim();
            if(productName == "")
            {
                return;
            }
            ProductManagement.GetInstance().SaveCheckPoint(productName,checkDic);
            MessageBox.Show("复测点保存完成","保存复测点",MessageBoxButton.OK,MessageBoxImage.Information);
        }


        //初始化复查点
        private void InitCheckPointList(string productname,string wavelength,Grid grid)
        {
            Dictionary<string, List<CheckPoint>> checkDic = ProductManagement.GetInstance().GetCheckPointDic(productname);
            if (!checkDic.ContainsKey(wavelength))
            {
                //如果本地配置文件没有相应的复查点，在左上角新建一个LinearControl
                LinearControl linearControl = new LinearControl();
                grid.Children.Add(linearControl);
                linearControl.HorizontalAlignment = HorizontalAlignment.Left;
                linearControl.VerticalAlignment = VerticalAlignment.Top;
                linearControl.Height = 30;
                return;
            }
            List<CheckPoint> checkList = checkDic[wavelength];
            for (int i = 0; i < checkList.Count; i++)
            {
                LinearControl linearControl = new LinearControl();
                grid.Children.Add(linearControl);
                linearControl.HorizontalAlignment = HorizontalAlignment.Left;
                linearControl.VerticalAlignment = VerticalAlignment.Top;
                linearControl.Height = 30;
                linearControl.Margin = new Thickness(0,35*i,0,0);
                linearControl.CheckPower = checkList[i].CheckPower.ToString();
                linearControl.Limit = checkList[i].Limit.ToString();
            }
        }


        //选中机型时，将它的机型名称、复查点显示在右侧
        private void ProductNameDataGrid_SelectedCellsChanged(object sender, SelectedCellsChangedEventArgs e)
        {
            int index = ProductNameDataGrid.SelectedIndex;
            if (index < 0)
            {
                return;
            }
            List<string> productnameList = ProductManagement.GetInstance().GetProductList();
            string productname = productnameList[index];
            //显示机型
            ProductNameBox.Text = productname;
            //添加波长对应的选项卡
            List<string> wavlengthList = ProductManagement.GetInstance().GetWaveList(productname);
            CheckPointTab.Items.Clear();
            foreach (string wavelength in wavlengthList) {
                TabItem tab = new TabItem();
                ScrollViewer scrollViewer = new ScrollViewer();
                tab.Content = scrollViewer;
                Grid grid = new Grid();
                scrollViewer.Content = grid;
                InitCheckPointList(productname,wavelength,grid);
                tab.Header = wavelength;
                CheckPointTab.Items.Add(tab);
                tab.IsSelected = true;
            }
            
        }
    }
}
